package com.go.timer;

import com.go.config.ApplicationContextConfig;
import com.go.core.bean.DdsScore;
import com.go.core.bean.JumpingScore;
import com.go.core.bean.RunningScore;
import com.go.dds.service.DdsService;
import com.go.dto.EventDto;
import com.go.enums.ApiCode;
import com.go.jumping.service.JumpService;
import com.go.running.service.RunningService;
import com.go.util.TimeUtil;
import com.go.websocket.MessageService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;

@Slf4j
public class MyTimerTask extends TimerTask {

    // 解决 @Autowired注解自动注入失败的问题
    MessageService messageService = (MessageService) ApplicationContextConfig.getBean("messageService");
    RunningService runningService = (RunningService) ApplicationContextConfig.getBean("runningService");
    JumpService jumpService = (JumpService) ApplicationContextConfig.getBean("jumpService");
    DdsService ddsService = (DdsService) ApplicationContextConfig.getBean("ddsService");

    private String type;

    public MyTimerTask(String type) {
        this.type = type;
    }

    @Override
    public void run() {
        log.error("timer 开始执行 ========");
        if (TimeUtil.success){ // 未收到结束游戏信号，自动发送结束游戏信号
            EventDto eventDto = new EventDto();
            eventDto.build(ApiCode.GAME_STOP);
            switch (type){
                case "run":
                    List<RunningScore> scoreBorad = runningService.getScoreBorad();
                    Map<String, Object> runningMap = new HashMap<>();
                    // 查询用户信息
                    RunningScore runningPlayer = runningService.findPlayerByUUID(TimeUtil.UUID);
                    runningMap.put("current",runningPlayer);
                    runningMap.put("scoreboard",scoreBorad);
                    eventDto.setData(runningMap);
                    messageService.sendSingnalToPC(1,new Gson().toJson(eventDto));
                    break;
                case "jump":
                    List<JumpingScore> scoreBorad1 = jumpService.getScoreBorad();
                    Map<String, Object> jumpMap = new HashMap<>();
                    // 查询用户信息
                    JumpingScore jumpPlayer = jumpService.findPlayerByUUID(TimeUtil.UUID);
                    jumpMap.put("current",jumpPlayer);
                    jumpMap.put("scoreboard",scoreBorad1);
                    eventDto.setData(jumpMap);
                    messageService.sendSingnalToPC(1,new Gson().toJson(eventDto));
                    break;
                case "dds":
                    List<DdsScore> scoreBorad2 = ddsService.getScoreBorad();
                    Map<String, Object> ddsMap = new HashMap<>();
                    // 查询用户信息
                    DdsScore playerByUUID = ddsService.findPlayerByUUID(TimeUtil.UUID);
                    ddsMap.put("current",playerByUUID);
                    ddsMap.put("scoreboard",scoreBorad2);
                    eventDto.setData(ddsMap);
                    messageService.sendSingnalToPC(1,new Gson().toJson(eventDto));
                    break;
                default:break;
            }
            log.error("timer 执行完毕");
        }else {
            log.error("timer 被销毁");
            System.gc(); // 垃圾回收机制
            cancel(); // 取消timertask
        }
    }
}
